import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from infomation import get_infomation
import random
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



# 创建浏览器对象
driver = webdriver.Chrome()
driver.get("https://tj.wuxianshixun.cn:8443/app/#/home")
driver.maximize_window()
time.sleep(1)

# 过登录界面
driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/form[1]/div[1]/input').send_keys("zjw")
driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/form[1]/div[2]/input').send_keys("zjw123")
element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@id="app"]/div/div[1]/div/form[1]/input'))
)
driver.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/div/form[1]/input').click()


df_data = pd.read_excel("东升6.23日门诊签约数据明细.xlsx", sheet_name='门诊日志')



while True:
    try:
        # 获取当前是第几条数据
        now_time = time.time()
        with open("日志/env1.txt", "r", encoding="utf-8") as f_re:
            num = int(f_re.readlines()[2].strip("\n").split(":")[-1])

            print(f"目前执行数为{num},正在执行第{num+1}个操作")
        # 获取该条数据对应的信息
        name,infomation_person = get_infomation('东升6.23日门诊签约数据明细.xlsx', '门诊日志',num+2)


        # 页面二输入
        time.sleep(1)
        input_ids = driver.find_element(By.TAG_NAME, 'form')
        id1 = input_ids.find_element(By.XPATH, './div[1]/div[2]/div[1]/div[1]/input')
        id1.clear()
        time.sleep(1)
        id1.send_keys(infomation_person)
        id2 = input_ids.find_element(By.XPATH, './div[2]/div[2]/div[1]/div[1]/input')
        id2.clear()
        time.sleep(1)
        id2.send_keys(name)
        id3 = input_ids.find_element(By.XPATH, './div[3]/div[2]/div[1]/div[1]/input')
        time.sleep(1)
        id3.send_keys(name)
        id3.clear()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="app"]/div/form/div[4]/div[2]/button[1]').click()

        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.CLASS_NAME, 'button-grid'))
        )



        # 页面跳转
        all_button = driver.find_element(By.CLASS_NAME, 'button-grid')
        buttons = all_button.find_elements(By.TAG_NAME, 'button')  # 获取所有button
        time.sleep(1)
        buttons[9].click()
        time.sleep(2)



        # 选择区域
        all_div_root = driver.find_element(By.TAG_NAME, 'form')
        divs = all_div_root.find_elements(By.XPATH, './div')
        time.sleep(1)


        # 清除所有已经选中的标签
        all_is_checkeds = all_div_root.find_elements(By.XPATH, '//label[@class="el-checkbox is-checked"]')
        for checked in all_is_checkeds:
            checked.click()
            time.sleep(1)
        # 清除第一个标题前内填入的文字
        divs[1].find_elements(By.TAG_NAME, 'label')[-1].click()
        element = divs[1].find_elements(By.TAG_NAME, 'input')[-1]
        element.clear()  # 先清除原有内容
        element.send_keys('腹部有异常')  # 再输入新内容

        # 点击并添加器官的病变标签
        type_gan = random.randint(2,7)
        divs[2].find_elements(By.XPATH, './div[2]/div[1]/label')[-1].click()
        time.sleep(0.5)
        if type_gan in [2, 4]:
            kuaizhuang = f"大小为{round(random.random(), 2)}"
            divs[2].find_elements(By.TAG_NAME, 'label')[-type_gan].click()
            element = divs[2].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(kuaizhuang)  # 再输入新内容
        else:
            chengdu = ["轻度", "中度", "重度"]
            numbers = random.randint(0, 2)
            divs[2].find_elements(By.TAG_NAME, 'label')[-type_gan].click()
            element = divs[2].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(chengdu[int(numbers)])  # 再输入新内容

        type_dan = random.randint(2,4)
        time.sleep(1)
        divs[3].find_elements(By.XPATH, './div[2]/div[1]/label')[-1].click()
        time.sleep(1)
        if type_dan in [3, 4]:
            kuaizhuang = f"大小为{round(random.random(), 2)}"
            divs[3].find_elements(By.TAG_NAME, 'label')[-type_dan].click()
            element = divs[3].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(kuaizhuang)  # 再输入新内容
        else:
            chengdu = ["轻度", "中度", "重度"]
            numbers = random.randint(0, 2)
            divs[3].find_elements(By.TAG_NAME, 'label')[-type_dan].click()
            element = divs[3].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(chengdu[int(numbers)])  # 再输入新内容


        type_yi = random.randint(2, 3)
        time.sleep(1)
        divs[4].find_elements(By.XPATH, './div[2]/div[1]/label')[1].click()
        time.sleep(1)
        divs[4].find_elements(By.TAG_NAME, 'label')[-type_yi].click()
        if type_yi in [2,3]:
            chengdu = ["轻度", "中度", "重度"]
            numbers = random.randint(0, 2)
            element = divs[4].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(chengdu[int(numbers)])  # 再输入新内容


        type_pi = random.randint(2, 4)
        time.sleep(1)
        divs[5].find_elements(By.XPATH, './div[2]/div[1]/label')[-1].click()
        if type_pi in [4]:
            kuaizhuang = f"大小为{round(random.random(), 2)}"
            divs[5].find_elements(By.TAG_NAME, 'label')[-type_pi].click()
            element = divs[5].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(kuaizhuang)  # 再输入新内容
        else:
            chengdu = ["轻度", "中度", "重度"]
            numbers = random.randint(0, 2)
            divs[5].find_elements(By.TAG_NAME, 'label')[-type_pi].click()
            element = divs[5].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(chengdu[int(numbers)])  # 再输入新内容

        type_shen = random.randint(2, 7)
        time.sleep(0.5)
        divs[6].find_elements(By.XPATH, './div[2]/div[1]/label')[-1].click()
        if type_shen in [6,7]:
            kuaizhuang = f"大小为{round(random.random(), 2)}"
            divs[6].find_elements(By.TAG_NAME, 'label')[-type_shen].click()
            element = divs[6].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(kuaizhuang)  # 再输入新内容
        else:
            chengdu = ["轻度", "中度", "重度"]
            numbers = random.randint(0, 2)
            divs[6].find_elements(By.TAG_NAME, 'label')[-type_shen].click()
            element = divs[6].find_elements(By.TAG_NAME, 'input')[-1]
            element.send_keys(chengdu[int(numbers)])  # 再输入新内容
        time.sleep(1)

        driver.find_element(By.XPATH, '//*[@id="app"]/div/form/div[10]/div[2]/button[1]').click()

        print(f'{name}提交成功')
        # 修改txt文件（显示执行到那个文件了）
        with open("日志/env1.txt", "r", encoding="utf-8") as f_re:
            num+=1
            lines = f_re.readlines()
            lines[0] = f"总操作数:{len(df_data.values)}\n"
            lines[1] = f"当前处理身份证号:{infomation_person}\n"
            lines[2] = f"已完成数量:{str(num)}\n"
        with open("日志/env1.txt", "w", encoding="utf-8") as f_wr:
            f_wr.writelines(lines)
        print(f'一条使用时间{time.time()-now_time}')



    except StopIteration:
        print("数据读取遇到了问题（1、可能是过程报错；2、可能是数据读取完成了）")
        break



time.sleep(100)
driver.quit()